package redis

import (
	"errors"
	"time"
)

func (r *Redis) Exists(key ...interface{}) (int64, error) {
	num, err := r.conn.Do("exists", key...)
	if err != nil {
		return 0, err
	}
	return num.(int64), err
}

func (r *Redis) TTL(key string) (int64, error) {
	seconds, err := r.conn.Do("ttl", key)
	if err != nil {
		return 0, err
	}
	s := seconds.(int64)
	if s == -2 {
		err := errors.New("key not exists")
		return 0, err
	}
	return s, err
}

func (r *Redis) Persist(key string) bool {
	_, err := r.conn.Do("persist", key)
	if err != nil {
		return false
	}
	return true
}

func (r *Redis) Expire(key string, ts time.Duration) bool {
	_, err := r.conn.Do("expire", key, ts.Seconds())
	return err == nil
}

func (r *Redis) ExpireAt(key string, expire time.Time) bool {
	_, err := r.conn.Do("expireat", key, expire.Unix())
	return err == nil
}
